﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
using HtmlDeploy.Processors;

namespace HtmlDeploy
{
    public class MasterPage
    {
        private readonly string masterPagePath;
        private readonly string viewPath;

        private readonly Dictionary<string, string> masterPages = new Dictionary<string, string>();

        public MasterPage(string masterPagePath, string viewPath)
        {
            this.masterPagePath = Path.GetFullPath(masterPagePath);
            this.viewPath = Path.GetFullPath(viewPath);

            InitMasterPages();
        }

        public Dictionary<string, string> Compile()
        {
            var processor = new MasterPageProcessor(masterPages);

            var di = new DirectoryInfo(viewPath);
            var files = di.GetFiles("*.aspx", SearchOption.AllDirectories).Select(x=> x.FullName);

            var result = files.ToDictionary(filePath => filePath, processor.Compile);

            return result;
        }
        
        public void CompileWithOutput(string outputPath)
        {
            outputPath = Path.GetFullPath(outputPath);
            var outputs = Compile();

            foreach (var output in outputs)
            {
                var filePath = outputPath + @"\" + output.Key.Substring(viewPath.Length);
                filePath = (filePath.Substring(0, filePath.LastIndexOf(".")) + ".html").Replace(@"\\", @"\");

                using(var sw = new StreamWriter(filePath))
                {
                    sw.WriteLine(output.Value);
                }
            }
        }

        public void CompileWithOutputAndTemplating(string outputPath, string jqueryTemplatePath)
        {
            outputPath = Path.GetFullPath(outputPath);
            jqueryTemplatePath = Path.GetFullPath(jqueryTemplatePath);
            var jqueryTemplate = new JqueryTemplate(jqueryTemplatePath).Process();

            var outputs = Compile();

            foreach (var output in outputs)
            {
                var filePath = outputPath + @"\" + output.Key.Substring(viewPath.Length);
                filePath = (filePath.Substring(0, filePath.LastIndexOf(".")) + ".html").Replace(@"\\", @"\");

                using(var sw = new StreamWriter(filePath))
                {
                    var doc = new HtmlDocument();
                    doc.LoadHtml(output.Value);

                    doc.OptionWriteEmptyNodes = true;

                    var tmpl = new HtmlDocument();
                    tmpl.LoadHtml(jqueryTemplate);

                    doc.DocumentNode.QuerySelector("body").AppendChild(tmpl.DocumentNode);
                    sw.WriteLine(doc.DocumentNode.InnerHtml);
                }
            }
        }

        private void InitMasterPages()
        {
            var di = new DirectoryInfo(masterPagePath);
            var files = di.GetFiles("*.Master", SearchOption.AllDirectories);

            if(files.Count() != files.Select(x=> x.Name).Count())
                throw new NotSupportedException("So far we only support uniquely named MasterPages.");

            foreach (var file in files)
            {
                using(var sr = file.OpenText())
                {
                    masterPages.Add(file.Name, sr.ReadToEnd());
                }
            }
        }
    }
}
